package cn.hd.service.impl;

import cn.hd.dao.BillDao;
import cn.hd.dao.RoleDao;
import cn.hd.dao.UserDao;
import cn.hd.entity.Bill;
import cn.hd.entity.User;
import cn.hd.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private BillDao billDao;

    @Override
    public int addUser(User user) {
        String str = UUID.randomUUID().toString().replaceAll("-","");
        user.setToken(str);
        return userDao.addUser(user);
    }

    @Override
    public int delUser(long id) {
        return userDao.delUser(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public User selectUserByName(String name) {
        return userDao.selectUserByName(name);
    }

    @Override
    public User selectUserById(long id) {
        return userDao.selectUserById(id);
    }

    @Override
    public PageInfo<User> selectAllUser(int pagenum, int pagesize, Map map) {
        PageHelper.startPage(pagenum,pagesize);
        List<User> users = userDao.selectUsers(map);
        for (User s : users) {
            String roleName = userDao.selectRoleById(s.getId());
            s.setJs(roleName);
        }
        PageInfo<User> userPageInfo = new PageInfo<>(users);

        return userPageInfo;
    }
    @Override
    public List<User> selectByRid(long rid) {
        return userDao.selectByRid(rid);
    }

    @Override
    public int updateUserRole(long id, long rid) {
        return userDao.updateUserRole(id,rid);
    }

    @Override
    public User selectByeRid(long rid) {
        return userDao.selectByeRid(rid);
    }

//  zjc--开始
    @Override
    public int updateUser2(User user) {
        return userDao.updateUser2(user);
    }
    @Override
    public int updateUserPassword(String password, long uid) {
        return userDao.updateUserPassword(password,uid);
    }

    @Override
    public Map selectzj(int uid) {
        List<Bill> bills = billDao.selectAllBillyzf(uid);
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        sp.format(date);
        List<String> da = new ArrayList<>();
        List<Double> jq = new ArrayList<>();
        int oneday=1000*60*60*24;
        for (int i = 1;i <= 7;i++){
            jq.add(0.0);
            da.add(sp.format(new Date(new Date().getTime() - (8-i)*oneday)));
        }
        List<Bill> billList = new ArrayList<>();
        for (Bill b : bills){
            for (int j = 0;j <= 6;j++){
                if (sp.format(b.getBegindate()).equals(da.get(j))){
                    jq.set(j,jq.get(j)+b.getPrice());
                }
            }
        }
        Map map = new HashMap();
        map.put("jq",jq);
        map.put("da",da);
        return map;
    }
}
